package com.xhj.xmly.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.xhj.xmly.bean.ListType;
import com.xhj.xmly.R;
import com.xhj.xmly.adapter.DiscoverListAdapter;
import com.xhj.xmly.callback.HttpService;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static android.R.id.list;

/**
 * A simple {@link Fragment} subclass.
 */
public class Discoer_list_frag extends Fragment implements Callback<ListType> {

    private DiscoverListAdapter adapter1,adapter2;
    private RecyclerView recyclerView1,recyclerView2;
    private List<ListType.DatasBean.ListBeanX> lists1,lists2;
    private ImageView head_iv;

    public Discoer_list_frag() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =inflater.inflate(R.layout.fragment_discoer_list_frag,container,false);
        recyclerView1= (RecyclerView) view.findViewById(R.id.fra_discover_list_Recycle1);
        recyclerView2= (RecyclerView) view.findViewById(R.id.fra_discover_list_Recycle2);
        head_iv= (ImageView) view.findViewById(R.id.fra_discover_list_iv_head);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init();
    }

    private void init() {
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://mobile.ximalaya.com/mobile/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        HttpService service=retrofit.create(HttpService.class);
        service.getform().enqueue(this);
        lists1 = new ArrayList<>();
        lists2 = new ArrayList<>();
        adapter1= new DiscoverListAdapter(getContext(),lists1);
        adapter2= new DiscoverListAdapter(getContext(),lists2);
        recyclerView1.setAdapter(adapter1);
        recyclerView2.setAdapter(adapter2);

    }


    @Override
    public void onResponse(Call<ListType> call, Response<ListType> response) {
        ListType listType = response.body();
        List<ListType.DatasBean.ListBeanX> list1 = listType.getDatas().get(0).getList();
        lists1.addAll(list1);
        List<ListType.DatasBean.ListBeanX> list2 = listType.getDatas().get(1).getList();
        lists2.addAll(list2);
        String url=listType.getFocusImages().getList().get(0).getPic();
        Picasso.with(getContext()).load(url).into(head_iv);
        adapter1.notifyDataSetChanged();
        adapter2.notifyDataSetChanged();

    }

    @Override
    public void onFailure(Call<ListType> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(getContext(),t.getMessage(),Toast.LENGTH_SHORT).show();
    }
}
